package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CartService;
import csu.web.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.midi.InvalidMidiDataException;
import java.io.IOException;

@WebServlet(name = "UpdateOrderServlet",urlPatterns = "/updateOrder")
public class UpdateOrderServlet extends HttpServlet {
    private static final String OrderSuccess = "/WEB-INF/jsp/order/orderSuccess.jsp";
    private static final String reUpdateOrder = "/WEB-INF/jsp/order/newOrder.jsp";
    private OrderService orderService;
    private CartService cartService;
    private AccountService accountService;
    private String msg;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //对购物车内物品信息和订单信息进行处理
        HttpSession session = req.getSession();

        session.setAttribute("action","updateOrder");

        //先获取用户输入
        String firstName = req.getParameter("receiverFirstName");
        String lastName = req.getParameter("receiverLastName");
        String address1 = req.getParameter("shipToAddress1");
        String address2 = req.getParameter("shipToAddress2");

        //检查输入是否为空
        msg = null;
        if(firstName==null||lastName==null||address1==null||address2==null){
            msg = "please finish the orders!";
            session.setAttribute("msg",msg);
            req.getRequestDispatcher(reUpdateOrder).forward(req,resp);
        }else if(firstName.isEmpty()||lastName.isEmpty()||address1.isEmpty()||address2.isEmpty()){
            msg = "please finish the orders!";
            session.setAttribute("msg",msg);
            req.getRequestDispatcher(reUpdateOrder).forward(req,resp);
        }

        //再把购物车账号和订单都拿到手 注册的账号没存session！！记得改一下
        Order order = new Order();
        orderService = new OrderService();
        Account account = (Account) session.getAttribute("loginAccount");
        Cart cart = (Cart) session.getAttribute("cart");
        //分配订单编号
        try {
            order.setOrderId(orderService.getNextId("ordernum"));
        } catch (InvalidMidiDataException e) {
            throw new RuntimeException(e);
        }
        order.initOrder(account,cart);
        order.setShipAddress1(address1);
        order.setShipAddress2(address2);
        order.setShipToFirstName(firstName);
        order.setShipToLastName(lastName);

        //添加订单到数据库
        try {
            orderService.insertOrder(order);
        } catch (InvalidMidiDataException e) {
            throw new RuntimeException(e);
        }

        //最后清空购物车
        cartService = new CartService();
        cartService.clearCart(account.getUsername());
        cart = new Cart();
        session.setAttribute("cart",cart);

        //转发至成功页面
        req.getRequestDispatcher(OrderSuccess).forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
